package com.fxsen.commons.core.base.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 请求响应体
 *
 * @author fxsen
 */
@ApiModel("响应结果")
public class ResponseResult implements Serializable {

	private static final long serialVersionUID = 8041766238120354183L;
	@ApiModelProperty(name = "状态码",example = "100")
	private int code;
	@ApiModelProperty("是否成功")
	private boolean success;
	@ApiModelProperty("描述信息")
	private String msg;
	@ApiModelProperty("响应数据")
	private Object data;

	public static ResponseResult OK() {
		return success(null);
	}
	public static ResponseResult success() {
		return success(null);
	}
	public static ResponseResult saveFailed(){
		return failed("保存失败！");
	}
	public static ResponseResult deleteFailed(){
		return failed("删除失败！");
	}
	public static ResponseResult updateFailed(){
		return failed("更新失败！");
	}
	public static ResponseResult ok(Object data) {
		return new ResponseResult()
				.setSuccess(ResponseStatus.SUCCESS.success)
				.setMsg(ResponseStatus.SUCCESS.valueZh)
				.setCode(ResponseStatus.SUCCESS.bCode)
				.setData(data);
	}
	public static ResponseResult ok(String msg,Object data) {
		return new ResponseResult()
				.setSuccess(ResponseStatus.SUCCESS.success)
				.setMsg(msg)
				.setCode(ResponseStatus.SUCCESS.bCode)
				.setData(data);
	}
	public static ResponseResult success(Object data) {
		return new ResponseResult()
				.setSuccess(ResponseStatus.SUCCESS.success)
				.setMsg(ResponseStatus.SUCCESS.valueZh)
				.setCode(ResponseStatus.SUCCESS.bCode)
				.setData(data);
	}
	public static ResponseResult success(String msg,Object data) {
		return new ResponseResult()
				.setSuccess(ResponseStatus.SUCCESS.success)
				.setMsg(msg)
				.setCode(ResponseStatus.SUCCESS.bCode)
				.setData(data);
	}
	public static ResponseResult failed(){
		return new ResponseResult().setSuccess(ResponseStatus.RUNTIME_EXCEPTION.success)
				.setMsg(ResponseStatus.RUNTIME_EXCEPTION.valueZh)
				.setCode(ResponseStatus.RUNTIME_EXCEPTION.bCode);
	}
	public static ResponseResult failed(String msg) {
		return failed(ResponseStatus.FAILED.bCode, msg);
	}

	public static ResponseResult failed(int code, String msg) {
		return new ResponseResult()
				.setSuccess(ResponseStatus.FAILED.success)
				.setMsg(msg)
				.setCode(code);
	}

	public int getCode() {
		return code;
	}

	public ResponseResult setCode(int code) {
		this.code = code;
		return this;
	}

	public boolean isSuccess() {
		return success;
	}

	public ResponseResult setSuccess(boolean success) {
		this.success = success;
		return this;
	}

	public String getMsg() {
		return msg;
	}

	public ResponseResult setMsg(String msg) {
		this.msg = msg;
		return this;
	}

	public Object getData() {
		return data;
	}

	public ResponseResult setData(Object data) {
		this.data = data;
		return this;
	}
}